var WxParse = require('../../wxParse/wxParse.js');
//获取应用实例
var app = getApp()
Page({
  data: {
    showLoading: true,
    noMoretip: false,
    page: 0,
    isShowfl:false,
    isShowDistance:false,
    curFlid:'',
    curType:'new',
    banner:[],
    region:['全国','全国'],
  },
  onLoad:function(e){
    var that = this;
    if (e && e.title) {
      app.setNavtitle(e.title);
    } else {
      app.setNavtitle('门店列表');
    }
    if (e&&e.id){
      that.setData({
        curFlid: e.id
      })
    }
    var location = app.globalData.location;
    if (location && location.lat) {
      console.log("1111111111111")
      that.setData({
        curChooseLatitude: location.lat,
        curChooseLongitude: location.lng
      })
      that.requestShoplist();
    } else {
      console.log("2222222222")
      that.getLocation();
    }
  },
  onShow: function () {
    var that = this;
    
  },
  requestCategory: function () {
    return;
    var that = this;
    wx.showLoading({
      title: '正在加载',
      mask: true,
      time: 100000
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_mobile_shop_apply_category'
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          that.setData({
            categoryList: res.data.data
          })
        } else {
          console.log(res.data)
          // app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  requestShoplist: function (name) {
    var that = this;
    that.setData({
      showLoading: true
    });
    var data = {};
    var isKeyword = that.data.isKeyword ? that.data.isKeyword : false;
    var page = that.data.page;
    data.map = 'applet_shop_list';
    data.sort = that.data.curType;
    data.cate = that.data.curFlid ? that.data.curFlid:'';
    data.page = page;
    data.lng = that.data.curChooseLongitude ? that.data.curChooseLongitude : 0;
    data.lat = that.data.curChooseLatitude ? that.data.curChooseLatitude : 0;
    if(that.data.region.length>0)
      data.area= that.data.region[1];
    if (isKeyword) {
      if (that.data.searchTxt) {
        data.name = that.data.searchTxt;
      }
    }
    console.log(data);
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {

        console.log(page);
        console.log(res.data);
        if (res.data.ec == 200) {
          var allArr = [];
          var initArr = that.data.shopList ? that.data.shopList : [];

          var curArr = res.data.data;
          var lastPageLength = curArr.length;
          if (page > 0) {
            allArr = initArr.concat(curArr);
          } else {
            allArr = res.data.data;
          }
          that.setData({
            shopList: allArr,
            banner:res.data.slide,
          })
          if (lastPageLength < 10) {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
          console.log(that.data.shopList);
        } else {
          if (page <= 0) {
            that.setData({
              shopList: [],
              noMoretip: false,
              showLoading: false
            })
          } else {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
        }
      },
      complete: function () {
        that.setData({
          showLoading: false
        });
        wx.stopPullDownRefresh();
      }
    });
  },
  getLocation: function (e) {
    var that = this;
    that.setData({
      showLocationtip: true
    })
    wx.getLocation({
      success: function (res) {
        console.log("333333333");
        console.log(res);
        that.setData({
          curChooseLatitude: res.latitude,
          curChooseLongitude: res.longitude,
          showLocationtip: false
        })
        var location = {
          lat: res.latitude,
          lng: res.longitude
        }
        app.globalData.location = location;
        that.requestShoplist();
      },
      fail: function () {
        console.log("444444444444");
        wx.getSetting({
          success: (res) => {
            console.log("5555555555");
            console.log(res);
            if (res.authSetting['scope.userLocation']) {
              that.setData({
                locationTips: "无法获取您的当前定位地址，请打开定位权限"
              })
              that.requestShoplist();
            } else {
              wx.openSetting({
                success: (res) => {
                  console.log("6666666");
                  res.authSetting = {
                    "scope.userLocation": true
                  }
                  console.log(res.authSetting);
                  that.onPullDownRefresh();
                },
                fail(f){
                  console.log("777777777");
                  console.log(f);
                  that.requestShoplist();
                  // that.onPullDownRefresh();
                }
              })
            }
          },
        })
      },
      complete: function () {
        // wx.hideLoading(); 
      }
    })
  },
  chooseLocation: function () {
    var that = this;
    wx.chooseLocation({
      success: (res) => {
        console.log(res);
        var curLocation = res.name ? res.name : res.address;
        that.setData({
          curLocation: curLocation,
          curChooseLatitude: res.latitude,
          curChooseLongitude: res.longitude,
          curShopCategory: '',
          showLocationtip: false
        })
        var location = {
          lat: res.latitude,
          lng: res.longitude
        }
        app.globalData.location = location;
        that.requestShoplist();
      },
      fail: (res) => {
        wx.getSetting({
          success: (res) => {
            if (res.authSetting['scope.userLocation']) {
              that.setData({
                locationTips: "无法获取您的当前定位地址，请打开定位权限"
              })
            } else {
              wx.openSetting({
                success: (res) => {
                  res.authSetting = {
                    "scope.userLocation": true
                  }
                  console.log(res.authSetting);
                  that.onPullDownRefresh();
                }
              })
            }
          }
        })
      }
    })
  },
  searchtxtChange: function (e) {
    var that = this;
    that.setData({
      searchTxt: e.detail.value
    })
    if (e.detail.value==''){
      that.setData({
        isKeyword: false
      })
    }
    console.log(that.data.searchTxt);
  },
  confirmSearch:function(){
    var that = this;
    that.setData({
      isKeyword:true
    })
    that.requestShoplist();
  },
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      page: 0,
      noMoretip: false,
      showLoading: true
    });
    that.onLoad();
  },
  onReachBottom: function () {
    var that = this;
    console.log("到达页面底部")
    var isMore = that.data.noMoretip;
    var page = that.data.page;
    page++;
    that.setData({
      page: page
    });
    if (isMore) {
      console.log("已完成或正在加载");
    } else {
      that.requestShoplist();
    }
  },
  makeCall: function (e) {
    var that = this;
    var mobile = e.currentTarget.dataset.mobile;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '',
      content: '拨打电话 ' + mobile,
      confirmText: '拨打',
      confirmColor: '#48C23D',
      success: function (res) {
        if (res.confirm) {
          wx.makePhoneCall({
            phoneNumber: mobile,
            success: function () {
              that.requestCall(id);
            },
            fail: function () {
              console.log("拨打电话失败！")

            }
          })
        }
      }
    })
  },
  toggleFlitem:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    var title = e.currentTarget.dataset.title;
    that.setData({
      curFlid:id,
      curType:'new',
      isShowfl:false,
      page: 0
    })
    app.setNavtitle(title);
    that.requestShoplist();
  },
  toggleSorttype: function (e) {
    var that = this;
    var type = e.currentTarget.dataset.type;
    that.setData({
      curType: type,
      page: 0
    })
    that.requestShoplist();
  },
  toTelcategory:function(){
    wx.navigateTo({
      url: '/subpages/telcategory/telcategory',
    })
  },
  toMycollect: function (e) {
    var type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '/subpages/myCollect/myCollect?type='+type,
    })
  },
  toggleShowfl:function(){
    var that = this;
    var isShowfl = !that.data.isShowfl;
    that.setData({
      isShowfl: isShowfl
    })
  },
  toggleShowDistance:function(){
    var that = this;
    var isShowDistance = !that.data.isShowDistance;
    that.setData({
      isShowDistance: isShowDistance
    })
  },
  hideFl:function(){
    this.setData({
      isShowfl: false,
      isShowDistance:false
    })
  },
  toShopInfoDetail: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/subpages/shopInfoDetail/shopInfoDetail?id=' + id,
    })
  },
  requestCall: function (id) {
    var that = this;
    var data = {
      map: 'applet_mobile_set_record',
      sid: id,
      type: 'mobile'
    };
    console.log(data);
    wx.showLoading({
      title: '加载中',
      mask: true,
      time: 100000
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          // app.errorTip(that, res.data.data.msg, 2000);
        } else {
          console.log(res.data)
          // app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
    this.requestShoplist();
  },
  openMap: function (e) {
    var latitude = e.currentTarget.dataset.lat;
    var longitude = e.currentTarget.dataset.lng;
    var address = e.currentTarget.dataset.address;
    var name = e.currentTarget.dataset.name;
    console.log(latitude + '--' + longitude + '--' + address + '--' + name)
    wx.openLocation({
      latitude: Number(latitude),
      longitude: Number(longitude),
      name: name,
      address: address,
      scale: 18
    })
  },
})
